package Rbac.service.array;

import Rbac.domain.dto.RoleDto;
import Rbac.domain.entity.Role;
import Rbac.domain.vo.RoleVo;
import Rbac.domain.vo.Result;
import Rbac.service.IRoleService;
import Rbac.service.IUsersService;

/**
 * 业务角色模块
 */
public class RoleService extends BaseService<Role, RoleDto, RoleVo> implements IRoleService {
    private IUsersService usersService;
    private static IRoleService singleton;
     private RoleService(){
         data= new Role[DATA_SIZE+1];
         temp= new RoleVo[DATA_SIZE];
    }
    public static IRoleService getInstance(){
        if(singleton==null){
            singleton=new RoleService();
        }
        return singleton;
    }
    @Override
    RoleVo convertToVo(Role role){
        return new RoleVo(role.getId(),role.name,role.dataScope);
    }
    @Override
    Role createEntity(RoleDto dto, int id){
        return new Role(id, dto.name, dto.dataScope);
    }
    @Override
    void updateEntity(RoleDto dto, Role role){
        role.name=dto.name;
        role.dataScope=dto.dataScope;
    }
    @Override
    public Result delete(int id){
        if(existsById(id)) {
            return Result.failure("删除失败，部门不存在！");
        }
        if(usersService==null){
            usersService=UsersService.getInstance();
        }
        if(usersService.roleInUse(id)){
            return Result.failure("删除失败，该角色已被使用！");
        }
        data[id]=null;
        return Result.success("删除成功！", null);
    }
}
